package com.android.webframework;

public class WebFramework {
	private RequestObject requestOb;
	private ResponseObject responseOb;
	private PHashtable table;
	private WebService wf;
	private WFLog wFLogging = new WFLog();
	

	public WebFramework() {
		// Create table of key and value pairs for the frameworks and selector pairs.
		wFLogging.appendToLog("Creating hashtable");
		table = new PHashtable();
		wFLogging.appendToLog("Inserting hashtable");
		table.insert("mp3", 1);
		table.insert("picture", 2);
		table.insert("directions", 3);
		table.insert("tts", 4);
		wFLogging.appendToLog("Empty response object");
		responseOb = null;
				
		wFLogging.appendToLog("Request object add");
	}

	public void request() throws RequestException {
		int key;
		wFLogging.appendToLog("Get key for object");
		try {
			key = (Integer)table.get(requestOb.getKey());
		}
		catch (ObjectNotFoundException onf) {
			onf.toString();
			key = 0;
		}
		
		wFLogging.appendToLog("Switch on key");
		switch (key) {
		case 1:
			wFLogging.appendToLog("key is mp3");
			MP3Service mf = new MP3Service(requestOb);
			wf = mf;
			break;
		case 2:
			wFLogging.appendToLog("key is picture");
			PictureService pf = new PictureService(requestOb);
			wf = pf;
			break;
		case 3:
			wFLogging.appendToLog("key is directions");
			DirectionsService df = new GoogleDirections(requestOb);
			wf = df;
			break;
		case 4:
			wFLogging.appendToLog("key is text to speech");
			TextToSpeechService tts = new TextToSpeechService(requestOb);
			wf = tts;
			break;
		default:
			wf = null;
			break;
		}
		
		try {
			responseOb = wf.request();
		}
		catch (RequestException re) {
			re.toString();
			responseOb = null;
		}
		catch (NullPointerException ne) {
			ne.toString();
			responseOb = null;
		}
	}
	
	public ResponseObject response() {
		return responseOb;
	}
	
	public void setRequestType(String key) {
        requestOb = new RequestObject();
        requestOb.setKey(key);
	}

	public void addParameter(String key, Object param) {
        requestOb.addParameter(key, param);
	}
}